#coding=utf8
from . import admin
from flask import render_template, redirect, url_for, session, request
from forms import LoginForm
from functools import wraps
from ..models import Activity, Publisher
from datetime import datetime
from .. import db
from ..utils import get_wechat_article
from .. import app
import os
# from flask_cachecontrol import dont_cache

# 内容发布者登录装饰器
def admin_login_req(f):
	@wraps(f)
	def login_req(*args, **kwargs):
		if 'admin_user' not in session:
			return redirect(url_for('admin.login', next=request.url))
		return f(*args, **kwargs)
	return login_req


@admin.route('/register')
def register():
	return "admin register"


@admin.route('/')
def index():
	return "Welcome to triangle area."


@admin.route('/admin/login', methods=["GET", "POST"])
def login():
	form = LoginForm()
	if form.validate_on_submit():
		data = form.data
		session['admin_user'] = data['admin_user']
		return redirect(url_for('admin.activity_management', page=1))
	return render_template('admin/login.html', form=form)

@admin.route('/admin/logout', methods=["GET"])
@admin_login_req
def logout():
	session.pop('admin_user', None)
	return redirect(url_for('admin.login'))


@admin.route('/admin/actlist/<int:page>', methods=["GET"])
@admin_login_req
def activity_management(page=None):
	if page == None:
		page = 1
	page_data = Activity.query.filter_by(
	).order_by(
		Activity.adding_time.desc()
	).paginate(page=page, per_page=8)
	# print page_data == None
	# for v in page_data.items:
	# 	print v
	return render_template('admin/activity_management.html', title=u'活动列表', page_data=page_data, page_num=page)

@admin.route('/admin/publist/<int:page>', methods=["GET"])
@admin_login_req
def publisher_management(page=None):
	if page == None:
		page = 1
	page_data = Publisher.query.filter_by(
	).order_by(
		Publisher.register_time.desc()
	).paginate(page=page, per_page=8)
	# print 'page:', page_data.page
	return render_template('admin/publisher_management.html', title=u'用户列表', page_data=page_data)

# 管理员查看活动详情
@admin.route('/admin/activity/<activity_id>', methods=["GET"])
@admin_login_req
def deal_activity(activity_id):
	activity = Activity.query.filter_by(
		id = activity_id
	).first()
	# print request.url
	page_num = request.args.get('page_num')
	return render_template('admin/activity_detail.html', activity=activity, page_num=page_num)

# 管理员通过该活动
@admin.route('/admin/activity/accept/<activity_id>', methods=["GET"])
@admin_login_req
def accept_activity(activity_id):
	activity = Activity.query.filter_by(
		id = activity_id
	).first()
	activity.new_state = 2
	activity.deal_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
	db.session.commit()

	filepath = os.path.join(app.config['WECHAT_HTML'], activity.id+'.html')
	get_wechat_article(activity.article_url, filepath)

	page_num = request.args.get('page_num')

	return render_template('admin/activity_detail.html', activity=activity, page_num=page_num)


# 管理员拒绝该活动
@admin.route('/admin/activity/reject/<activity_id>', methods=["GET"])
@admin_login_req
def reject_activity(activity_id):
	activity = Activity.query.filter_by(
		id = activity_id
	).first()
	activity.new_state = 1
	activity.deal_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
	db.session.commit()

	page_num = request.args.get('page_num')
	# print page_num
	return render_template('admin/activity_detail.html', activity=activity, page_num=page_num)

